﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using DissipativeInputProcessing;
using Control = System.Windows.Controls.Control;
using TextBox = System.Windows.Controls.TextBox;

namespace ComputationVisualizer
{
    class CommonPaletteCreator
    {
        private TextBox _paletteMinValTB;
        private TextBox _paletteMaxValTB;
        private Control _paletteMinBtn;
        private Control _paletteMaxBtn;
        private ColorConverter _colorConverter;
        private ColorDialog colorDialog = new ColorDialog();
        private event RoutedEventHandler _updatedEvent;

        public event RoutedEventHandler PaletteUpdated
        {
            add { _updatedEvent += value; }
            remove { _updatedEvent -= value; }
        }

        public CommonPaletteCreator(TextBox paletteMinValTB, TextBox paletteMaxValTB, Control paletteMinBtn, Control paletteMaxBtn)
        {
            // TODO: Complete member initialization
            _paletteMinValTB = paletteMinValTB;
            _paletteMaxValTB = paletteMaxValTB;
            _paletteMinBtn = paletteMinBtn;
            _paletteMaxBtn = paletteMaxBtn;
            _colorConverter = new ColorConverter();
            _paletteMaxBtn.PreviewMouseDown += new System.Windows.Input.MouseButtonEventHandler(SetColor);
            _paletteMinBtn.PreviewMouseDown += new System.Windows.Input.MouseButtonEventHandler(SetColor);
            _paletteMinValTB.TextChanged += new System.Windows.Controls.TextChangedEventHandler(PaletteTB_TextChanged);
            _paletteMaxValTB.TextChanged += new System.Windows.Controls.TextChangedEventHandler(PaletteTB_TextChanged);
        }

        void PaletteTB_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            if(sender is TextBox)
            {
                double result = 0.0;
                if (Double.TryParse(((TextBox)sender).Text, out result))
                    if (_updatedEvent != null)
                        _updatedEvent(this, null);
            }
        }

        void SetColor(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (sender is Control)
            {
                if (colorDialog.ShowDialog() == DialogResult.OK)
                {
                    ((Control)sender).Background = new SolidColorBrush(_colorConverter.DrawingColorToWpfColor(colorDialog.Color));
                    if (_updatedEvent != null)
                        _updatedEvent(this, null);
                }
            }
        }
        public FramePalette GenerateCurrentPalette()
        {
            try
            {
                var palette = new FramePalette
                {
                    MinColor =
                        _colorConverter.WpfColorToDrawingColor(
                            ((SolidColorBrush)_paletteMinBtn.Background).Color),
                    MaxColor =
                        _colorConverter.WpfColorToDrawingColor(
                            ((SolidColorBrush)_paletteMaxBtn.Background).Color),
                    MinValue = Single.Parse(_paletteMinValTB.Text),
                    MaxValue = Single.Parse(_paletteMaxValTB.Text)
                };
                if (palette.MaxValue < palette.MinValue)
                    throw new FormatException();
                return palette;
            }
            catch (Exception)
            {
                return null;
            }   
        }
        public void SetPalette(FramePalette palette)
        {
            if (palette != null)
            {
                _paletteMaxBtn.Background = new SolidColorBrush(_colorConverter.DrawingColorToWpfColor(palette.MaxColor));
                _paletteMinBtn.Background = new SolidColorBrush(_colorConverter.DrawingColorToWpfColor(palette.MinColor));
                _paletteMaxValTB.Text = palette.MaxValue.ToString();
                _paletteMinValTB.Text = palette.MinValue.ToString();
            }
        }

        ~CommonPaletteCreator()
        {
            _paletteMaxBtn.PreviewMouseDown -= new MouseButtonEventHandler(SetColor);
            _paletteMinBtn.PreviewMouseDown -= new MouseButtonEventHandler(SetColor);
            _paletteMinValTB.TextChanged -= new System.Windows.Controls.TextChangedEventHandler(PaletteTB_TextChanged);
            _paletteMaxValTB.TextChanged -= new System.Windows.Controls.TextChangedEventHandler(PaletteTB_TextChanged);
        }
    }
}
